<template>
  <div class="app-container">
    <div class="filter-container">
      <label style="color:#909399;font-weight:500;">任务编号</label>
      <el-input
        v-model="queryParams.id"
        placeholder="请输入任务编号"
        class="filter-item search-item"
      />
      <label style="color:#909399;font-weight:500;">司机名称</label>
      <el-input
        v-model="queryParams.name"
        placeholder="请输入司机名称"
        class="filter-item search-item"
      />
      <label style="color:#909399;font-weight:500;">任务状态</label>
      <el-select v-model="queryParams.status" placeholder="请选择">
        <el-option label="待发车" value="1"></el-option>
        <el-option label="在途" value="2"></el-option>
        <el-option label="已到达" value="4"></el-option>
      </el-select>
      <el-button
        style="background-color: #E05635;color: #fff;border-radius: 5px;border-color: #DCDFE6;"
        @click="search"
      >{{ $t('table.search') }}</el-button>
      <el-button
        style="background-color: #fff;color: #606266;;border-radius:5px;border-color: #DCDFE6;"
        @click="reset"
      >{{ $t('table.reset') }}</el-button>
      <el-dropdown
        v-has-any-permission="['station:add','station:delete','station:export']"
        class="filter-item"
        trigger="click"
      >
        <el-button
          style="height:40px;margin-top:6px;background-color: #fff;color: #606266;border-color: #DCDFE6"
        >
          {{ $t('table.more') }}
          <i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-has-permission="['transport-task:add']"
            @click.native="add"
          >{{ $t('table.add') }}</el-dropdown-item>
          <el-dropdown-item
            v-has-permission="['transport-task:delete']"
            @click.native="batchDelete"
          >{{ $t('table.delete') }}</el-dropdown-item>
          <el-dropdown-item
            v-has-permission="['transport-task:export']"
            @click.native="exportExcel"
          >{{ $t('table.export') }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-card shadow="never" style="margin-top: 10px;">
      <el-table
        :key="tableKey"
        ref="table"
        stripe
        v-loading="loading"
        :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
        :header-cell-style="{background:'#FCFBFF',border:'0'}"
        fit
        style="width: 100%;"
        @selection-change="onSelectChange"
        @sort-change="sortChange"
      >
        <el-table-column align="center" type="selection" width="40px" />
        <el-table-column
          label="序号"
          :show-overflow-tooltip="true"
          align="center"
          prop="name"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="运输任务编号"
          :show-overflow-tooltip="true"
          align="center"
          prop="id"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车牌号码" align="center" width="100px">
          <template slot-scope="scope">
            <span>{{ scope.row.licensePlate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="调度机构" align="center" width="100px">
          <template slot-scope="scope">
            <span>{{ scope.row.orgId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="起始地" align="center" width="180px">
          <template slot-scope="scope">
            <span>{{ scope.row.startAgency }}</span>
          </template>
        </el-table-column>
        <el-table-column label="目的地" align="center" width="180px">
          <template slot-scope="scope">
            <span>{{ scope.row.endAgency }}</span>
          </template>
        </el-table-column>
        <el-table-column label="司机" align="center" width="100px">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="运输任务状态"
          class-name="status-col"
          width="70px"
        >
          <template slot-scope="{row}">
            <!--<el-tag
              :type="row.status | statusFilter"
            >{{ row.status ? $t('common.status.valid') : $t('common.status.invalid') }}</el-tag>-->
            <span v-if="row.status==1">未发车</span>
            <span v-if="row.status==2">运输中</span>
            <span v-if="row.status==3">待确认</span>
            <span v-if="row.status==4">已到达</span>
            <span v-if="row.status==5">已取消</span>
          </template>
        </el-table-column>
        <el-table-column
          label="车辆装载状态"
          class-name="status-col"
          width="70px"
        >
          <template slot-scope="{row}">
            <!--<el-tag
              :type="row.status | statusFilter"
            >{{ row.status ? $t('common.status.valid') : $t('common.status.invalid') }}</el-tag>-->
            <span v-if="row.loadingStatus==1">半载</span>
            <span v-if="row.loadingStatus==2">满载</span>
            <span v-if="row.loadingStatus==3">空载</span>
          </template>
        </el-table-column>
        <el-table-column
          label="计划提货时间"
          align="center"
          prop="createTime"
          sortable="custom"
          width="160px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.planPickUpGoodsTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="实际提货时间"
          align="center"
          prop="createTime"
          sortable="custom"
          width="160px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.actualPickUpGoodsTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="计划发车时间"
          align="center"
          prop="createTime"
          sortable="custom"
          width="160px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.planDepartureTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="实际发车时间"
          align="center"
          prop="createTime"
          sortable="custom"
          width="160px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.actualDepartureTime }}</span>
          </template>
        </el-table-column>
        <!--<el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
            <el-button type="text" size="small">编辑</el-button>
          </template>
        </el-table-column>-->
        <el-table-column
          :label="$t('table.operation')"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
          width="150px"
        >
          <template slot-scope="{row}">
              <!--<i @click="edit(row)" class="el-icon-edit table-operation" style="color: #2db7f5;" v-hasPermission="['station:update']" />
            <i @click="singleDelete(row)" class="el-icon-delete table-operation" style="color: #f50;" v-hasPermission="['station:delete']" />-->
            <a style="color: #009EFF;" @click="details(row)">查看运输任务详情</a>

          </template>
        </el-table-column>
      </el-table>
      <!--<pagination
        v-show="tableData.total>0"
        :limit.sync="pagination.size"
        :page.sync="pagination.current"
        :total="Number(tableData.total)"
        @pagination="fetch"
      />-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[3,5,10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length">
        </el-pagination>
      <transport-task-edit
        ref="edit"
        :dialog-visible="dialog.isVisible"
        :type="dialog.type"
        @close="editClose"
        @success="editSuccess"
      />
    </el-card>
  </div>
</template>

<script>
//import '@riophae/vue-treeselect/dist/vue-treeselect.css'
//import Pagination from '@/components/Pagination'
import TransportTaskEdit from './Edit'
import transportTaskApi from '@/api/TransportTask.js'

export default {
  name: 'TransportTaskManage',
  components: {  TransportTaskEdit },
  filters: {
    statusFilter(status) {
      const map = {
        false: 'danger',
        true: 'success'
      }
      return map[status] || 'success'
    }
  },
  data() {
    return {
      dialog: {
        isVisible: false,
        type: 'add'
      },
      tableKey: 0,
      // total: 0,
      orgList: [],
      queryParams: {},
      sort: {},
      selection: [],
      // 以下已修改
      loading: false,
      tableData: {
        total: 0
      },
      pageSize:3,
      currentPage:1
    }
  },
  computed: {},
  watch: {
    $route() {
      if (this.$route.path === '/transport/transport-task/page') {
          this.fetch()
      }
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    handleSizeChange(val) {
        this.pageSize=val;
    },
    handleCurrentChange(val) {
      this.currentPage=val;
    },

    loadListOptions({ callback }) {
      callback()
    },
    filterStatus(value, row) {
      return row.status === value
    },
    editClose() {
      this.dialog.isVisible = false
    },
    editSuccess() {
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      })
    },
    reset() {
      this.queryParams = {}
      this.sort = {}
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    exportExcel() {
      this.$message({
        message: '待完善',
        type: 'warning'
      })
    },
    singleDelete(row) {
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning'
        })
        return
      }
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      })
        .then(() => {
          const ids = []
          this.selection.forEach(u => {
            ids.push(u.id)
          })
          this.delete(ids)
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    delete(ids) {
      transportTaskApi.delete({ ids: ids }).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.$message({
            message: this.$t('tips.deleteSuccess'),
            type: 'success'
          })
        }
        this.search()
      })
    },
    add() {
      this.dialog.type = 'add'
      this.dialog.isVisible = true
      this.$refs.edit.setStation(false, this.orgList)
    },
    edit(row) {
      this.$refs.edit.setStation(row, this.orgList)
      this.dialog.type = 'edit'
      this.dialog.isVisible = true
    },
    fetch(params = {}) {
      this.loading = true
      //params.size = this.pagination.size
      //params.current = this.pagination.current
      console.log(params)
      transportTaskApi.page(params).then(response => {

        this.tableData = response.data
        this.loading = false
        //this.tableData.total=res.length
        //this.tableData = res.data
      })
    },
    sortChange(val) {
      this.sort.field = val.prop
      this.sort.order = val.order
      this.search()
    },
    details(row){
        this.$router.push({path:"/transport-task/details",query:{data:row}})
    }
  }
}
</script>
<style lang="scss">
.el-table {
  border: 1px solid #f7f6f9;
}
.el-table tr,
.el-table td {
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #f7f6f9;
}
</style>
